package demo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class MyLogAspect {
    //@Pointcut("@annotation(demo.MyLog)")
    @Pointcut("execution(* demo..*.*(..))")
    public void controllerAspect(){

    }


    /*@Before("controllerAspect()")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        System.out.println(methodName+"---start");
    }
    @After("controllerAspect()")
    public void afterMethod(JoinPoint joinPoint){
        System.out.println(joinPoint.getSignature().getName()+"..........end");
    }*/
    @Around("controllerAspect()&&@annotation(MyLog)")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        Logger logger=LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringType());
        logger.debug(joinPoint.getSignature().getName()+"...around");
        //System.out.println(joinPoint.getSignature().getName()+"...around");
        try {
            return joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return throwable.getMessage();
        }
    }

    @Before("controllerAspect()&&@annotation(ControllerWebLog)")
    public void outParam(JoinPoint joinPoint){
        Logger logger=LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringType());
        long startTime=System.currentTimeMillis();
        //System.out.println("startTime:"+startTime+"methodName:"+joinPoint.getSignature().getName());
        logger.debug("startTime:"+startTime+"methodName:"+joinPoint.getSignature().getName());
    }
}
